//
//  NSString+RCDES.m
//  StructToolsDemo
//
//  Created by 杨晓辉 on 2017/8/23.
//  Copyright © 2017年 杨晓辉. All rights reserved.
//

#import "NSString+RCDES.h"
#import "NSData+RCDES.h"
@implementation NSString (RCDES)


+ (NSString *)encryptDESWithKey:(NSString*)key andOffset:(NSString*)offset andSourceString:(NSString*)sourceString
{
    NSData * sourceStringData = [sourceString dataUsingEncoding:NSUTF8StringEncoding];
    NSData  *encryptData = [NSData encryptDESWithKey:key andOffset:offset andSourceData:sourceStringData];
    NSString * encryptStr =[encryptData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    
    return encryptStr;
    
}

+ (NSString *)decryptDESWithKey:(NSString*)key andOffset:(NSString*)offset andSourceString:(NSString*)sourceString
{
    NSData * encryptData = [[NSData alloc] initWithBase64EncodedString:sourceString options:NSDataBase64DecodingIgnoreUnknownCharacters];
    NSData * decryptData = [NSData decryptDESWithKey:key andOffset:offset andSourceData:encryptData];
    NSString * decryptStr =  [[NSString alloc] initWithData:decryptData encoding:NSUTF8StringEncoding];
    return decryptStr;
}


/**
 3DES 解密
 
 @param key 密钥
 @param sourceString 待解密数据
 @return 解密后的数据
 */
+ (NSString*)encrypt3DESWithKey:(NSString*)key andOffset:(NSString*)offset andSourceString:(NSString*)sourceString
{
      NSData * sourceStringData = [sourceString dataUsingEncoding:NSUTF8StringEncoding];
    NSData  *encryptData = [NSData encrypt3DESWithKey:key andOffset:offset andSourceData:sourceStringData];
    NSString * encryptStr =[encryptData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    return encryptStr;
    
}


+ (NSString*)decrypt3DESWithKey:(NSString*)key andOffset:(NSString*)offset andSourceString:(NSString*)sourceString
{
    NSData * encryptData = [[NSData alloc] initWithBase64EncodedString:sourceString options:NSDataBase64DecodingIgnoreUnknownCharacters];
    NSData * decryptData = [NSData decrypt3DESWithKey:key andOffset:offset andSourceData:encryptData];
    NSString * decryptStr =  [[NSString alloc] initWithData:decryptData encoding:NSUTF8StringEncoding];
    return decryptStr;
    

    
}

@end
